﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using PSDLib.Models;
using CaseManagerLibrary.Models;
using PSDLib;

namespace PSDManager
{
    public partial class TaskHistoryPanel : UserControl, IDataPanel
    {
        public ManagerHelper ManagerHelper { get; set; }
        public bool ShowListCheckBox
        {
            get { return lvwTask.CheckBoxes; }
            set { lvwTask.CheckBoxes = value; }
        }

        public string Summary
        {
            get { return this.lblSummary.Text; }
            set { this.lblSummary.Text = value; }
        }

        public TaskHistoryPanel()
        {
            InitializeComponent();

            this.toolStrip1.Visible = false;
            this.ShowListCheckBox = false;
            this.paging.PageSize = 50;
            this.paging.OnPageChanged += Paging_OnPageChanged;
            this.lvwTask.SelectedIndexChanged += LvwTask_SelectedIndexChanged;
        }

        private void LvwTask_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lvwTask.SelectedItems.Count > 0)
            {
                this.toolStrip1.Items["刻录光盘"].Visible = (lvwTask.SelectedItems[0].Tag as dl_task).task_type == 1;
            }
            else
            {
                this.toolStrip1.Items["刻录光盘"].Visible = false;
            }
        }

        public void AddCustomAction(string name, Image image, Action<ToolStripButton, ListView> action, bool visible = true)
        {
            var miAction = new ToolStripButton();
            miAction.Text = name;
            miAction.Name = name;
            miAction.Image = image;
            miAction.DisplayStyle = ToolStripItemDisplayStyle.ImageAndText;
            miAction.TextImageRelation = TextImageRelation.ImageAboveText;
            miAction.Click += new EventHandler(delegate (object sender, EventArgs e)
            {
                action?.Invoke(miAction, lvwTask);
            });
            miAction.Visible = visible;
            this.toolStrip1.Items.Add(miAction);
            this.toolStrip1.Visible = true;
        }

        private void Paging_OnPageChanged(int pageNo, int pageSize)
        {
            PageResult<dl_task> taskList = null;
            this.lvwTask.SuspendLayout();
            this.lvwTask.Items.Clear();


            taskList = ManagerHelper.GetTaskHistory(pageNo, pageSize);

            int total = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(taskList.Count) / pageSize));
            paging.Total = total > 0 ? total : 1;

            //OnUpdateSummary?.Invoke(Task, Channel, files.Count);

            if (taskList.Entities != null)
            {
                int index = 1;
                foreach (dl_task f in taskList.Entities)
                {
                    string status = "";
                    if (f.download_state == 0)
                    {
                        status += "等待下载";
                    }
                    else if (f.download_state == 1)
                    {
                        status += "正在下载";
                    }
                    else if (f.download_state == 2)
                    {
                        status += "下载完成：成功" + f.file_count + "个，失败" + f.error_count + "个文件";
                    }

                    lvwTask.Items.Add(new ListViewItem(new string[] {
                            (index + (pageNo-1)*pageSize).ToString(),
                            f.task_name,
                            f.start_time.ToString("yyyy-MM-dd HH:mm:ss") + " 至 " + (f.end_time==null ? "" : f.end_time.Value.ToString("yyyy-MM-dd HH:mm:ss")),
                            status
                        })).Tag = f;

                    index += 1;
                }
            }

            this.lvwTask.ResumeLayout();
        }

        public void RefreshData(params object[] parameter)
        {
            paging.PageNo = 1;
        }

        public void Reset(bool immediate = true)
        {
            Summary = "";
            lvwTask.Items.Clear();
            paging.Reset();
        }
    }
}
